#ifndef __DR16_H
#define __DR16_H

#include <usart.h>
#include "dmalib.h"
#include "main.h"
#include "user_lib.h"

#define DR16_RECEIVE_SIZE 32    // DMA 接收长度
#define DR16_EVENT_ONLINE 0x01  // 遥控器在线事件

#define KEY_W     (0x01U)
#define KEY_S     (0x01U << 1)
#define KEY_A     (0x01U << 2)
#define KEY_D     (0x01U << 3)
#define KEY_SHIFT (0x01U << 4)
#define KEY_CTRL  (0x01U << 5)
#define KEY_Q     (0x01U << 6)
#define KEY_E     (0x01U << 7)
#define KEY_R     (0x01U << 8)
#define KEY_F     (0x01U << 9)
#define KEY_G     (0x01U << 10)
#define KEY_Z     (0x01U << 11)
#define KEY_X     (0x01U << 12)
#define KEY_C     (0x01U << 13)
#define KEY_V     (0x01U << 14)
#define KEY_B     (0x01U << 15)

#define SW_UP   1
#define SW_MID  3
#define SW_DOWN 2

#define BUTTON_RIGHT (0x01U)
#define BUTTON_LEFT  (0x01U << 1)

struct __RC {
    uint16_t left_sw_;
    uint16_t right_sw_;

    int16_t left_x_;
    int16_t left_y_;
    int16_t right_x_;
    int16_t right_y_;

    int16_t wheel_;

};  //通道映射

struct __PC {
    int16_t x_;
    int16_t y_;
    int16_t z_;
    uint8_t button_;  // bit0: Right button, bit1: Left button
    uint8_t pressing_button_;
    uint8_t press_down_button_;

    uint16_t v_;
    uint16_t pressing_v_;
    uint16_t press_down_v_;
    uint16_t clicked_v_;

};  //通道映射
#if !(USE_SBUS)

class DR16 {
   public:
    DR16(void);

    void init(void);
    void decode(void);
    void reset(void);
    __RC get_rc(void);
    __PC get_pc(void);

    bool is_button_pressing(uint8_t button);
    bool is_button_pressed_down(uint8_t button);

    bool is_key_pressed_down(uint16_t key);
    bool is_key_clicked(uint16_t key);
    bool is_key_pressing(uint16_t key);

    void debug(void);

    UART_HandleTypeDef *huart_;                            // 遥控器接收端串口
    rt_err_t            event_recv(void);                  // 遥控器离线事件
    static void         rc_thread_entry(void *parameter);  // 遥控器离线检测线程函数
    rt_thread_t         rc_thread;                         // 遥控器离线检测线程
    rt_event_t          event;                             // 遥控器在线事件
    uint8_t             data[32];                          // 遥控器缓存数据

    __RC rc_;  //遥控器数据
    __PC pc_;  //键鼠数据

};  //接收机

extern DR16 dr16;

#endif

#endif
